package com.whmskj.xjlhsc.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.whmskj.xjlhbc.core.Page;
import com.whmskj.xjlhsc.entity.ShachangSbStoptime;
import com.whmskj.xjlhsc.entity.Stopreasondeclaration;
import com.whmskj.xjlhsc.mapper.ShachangSbStoptimeMapper;
import com.whmskj.xjlhsc.mapper.StopreasondeclarationMapper;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.vo.JtTcPh;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.vo.RunStopCarVo;
import com.whmskj.xjlhsc.mobie.equipmentManage.entity.vo.SbStopTimeVo;
import com.whmskj.xjlhsc.service.ShachangSbStoptimeService;
import com.whmskj.xjlhsc.service.StopreasondeclarationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author 张
 * @version 1.0
 */

@Service
public class ShachangSbStoptimeServiceImpl extends ServiceImpl<ShachangSbStoptimeMapper, ShachangSbStoptime> implements ShachangSbStoptimeService {
    @Autowired
    private ShachangSbStoptimeMapper shachangSbStoptimeMapper;
    @Autowired
    private StopreasondeclarationMapper stopreasondeclarationMapper;

    /**
     * 停车记录
     *
     * @param process
     * @return
     */
    @Override
    public List<SbStopTimeVo> stopList(String process, String keyWord) {
        Page.startPage();
        List<SbStopTimeVo> list = new ArrayList<>();
        list = shachangSbStoptimeMapper.selectSbStopTimeVo(process, keyWord);
//        List<SbStopTimeVo> sbStopTimeVos = stopreasondeclarationMapper.selectStopCarList(process,keyWord);
//        if (sbStopTimeVos != null)
//        list.addAll(sbStopTimeVos);
        list.sort(new Comparator<SbStopTimeVo>() {
            @Override
            public int compare(SbStopTimeVo o1, SbStopTimeVo o2) {
                return o1.getStarttime().compareTo(o2.getStarttime());
            }
        });
        return list;
    }

    @Override
    public List<Object> runStopCar(String process, String startTime, String endTime) {
        List<RunStopCarVo> list = shachangSbStoptimeMapper.SelectrunStopCar(process, startTime, endTime);

        Map<Integer, List<RunStopCarVo>> collect = list.stream()
                .collect(Collectors.groupingBy(RunStopCarVo::getId, LinkedHashMap::new,  // 保持插入顺序
                        Collectors.toList()));
        Set<Integer> keyS = collect.keySet();

        ArrayList<Object> list2 = new ArrayList<>();
        for (Integer key : keyS) {
            Duration totalDuration = Duration.ZERO;
            List<RunStopCarVo> list1 = collect.get(key);
            int count = 0;
            if (CollectionUtil.isNotEmpty(list1)) {
                for (RunStopCarVo runStopCarVo : list1) {
                    Duration between = Duration.between(LocalTime.MIN, runStopCarVo.getHs());
                    totalDuration = totalDuration.plus(between);
                    if (totalDuration.getSeconds() > 0) {
                        count++;
                    }
                }
                long totalHours = totalDuration.toHours();              // 总小时数（整数部分）
//                long totalMinutes = totalDuration.toMinutes();          // 总分钟数
                long totalSeconds = totalDuration.getSeconds();         // 总秒数
                long remainingMinutes = totalDuration.toMinutes() % 60; // 剩余分钟（排除整小时部分）

                Double data = Double.parseDouble(String.format("%.2f", (totalSeconds + 0.0) / 43200 * 100));
                HashMap<String, Object> hashMap = new HashMap<>();
                hashMap.put("hs", totalHours + "小时" + remainingMinutes + "分钟" + totalSeconds % 60 + "秒");
                hashMap.put("size", count);
                hashMap.put("list", list1);
                hashMap.put("name", list1.get(0).getDeviceName() + list1.get(0).getPlatform().replace("#", ""));
                hashMap.put("data", data);
                list2.add(hashMap);
            }
        }
        return list2;
    }

    @Override
    public List<Map<String, Object>> bantcfx(String process, String startTime, String endTime) {
        List<Map<String, Object>> list = shachangSbStoptimeMapper.selectBanTcFX(process, startTime, endTime);
        return list;
    }

    /**
     * 机台停车排行
     *
     * @param process
     * @param Time
     * @param type
     * @return
     */
    @Override
    public List<JtTcPh> jttcph(String process, String Time, String type) {
        List<JtTcPh> list = shachangSbStoptimeMapper.selectJtTcPh(process, Time, type);
        for (JtTcPh jtTcPh : list) {
            jtTcPh.setPlatform(jtTcPh.getPlatform().replace("#", ""));
        }
        JtTcPh maxJtTcPh = list.get(0);
        for (JtTcPh jtTcPh : list) {
            if (jtTcPh.getData().compareTo(maxJtTcPh.getData()) > 0) {
                maxJtTcPh = jtTcPh;
            }
        }
        BigDecimal bigDecimal = BigDecimal.valueOf(maxJtTcPh.getData());
        for (JtTcPh jtTcPh : list) {
            if (jtTcPh.getData() == 0) {
                continue;
            }
            double data = new BigDecimal(jtTcPh.getData()).divide(bigDecimal, 4, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            jtTcPh.setData(data);
        }
        return list;
    }


    /**
     * 班组停车统计
     *
     * @param process
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public List<Map<String, Object>> banzutctj(String process, String startTime, String endTime) {
        List<Map<String, Object>> list = shachangSbStoptimeMapper.selectBanzuiTcFx(process, startTime, endTime);
        return list;
    }

    @Override
    public List<Map<String, Object>> getPCMonth() {
        return shachangSbStoptimeMapper.selectPCMonth();
    }

}
